package org.shreker.plugins.test.view;

import com.intellij.ui.components.JBList;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

/**
 * @Author: ShrekerNil
 * @Date: 2018-10-31 17:00
 * @Description:
 */
public class SelectTableWindow extends JFrame {
    
    private Set<String> selectedTableNames;
    
    public SelectTableWindow(List<String> source) {
        this.setLayout(new GridLayout(1, 3));
        JBList<String> table = new JBList<>(source);
        table.setBorder(BorderFactory.createTitledBorder("which country do you want to"));
        Container container = this.getContentPane();
        container.add(table);
        container.add(new JScrollPane(table)); //对list1添加滚动条
        this.setLocationRelativeTo(null);
        this.setSize(400, 200);
        
        table.addListSelectionListener(event -> {
            JBList list = (JBList) event.getSource();
            String value = (String) list.getSelectedValue();
            this.getSelectedTableNames().add(value);
            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~");
        });
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("this.prent");
            }
        });
    }
    
    public Set<String> getSelectedTableNames() {
        if (selectedTableNames == null) {
            selectedTableNames = new HashSet<>();
        }
        return selectedTableNames;
    }
    
    public void showWindow() {
        // 显示窗口，前面创建的信息都在内存中，通过 this.setVisible(true) 把内存中的窗口显示在屏幕上。
        this.setVisible(true);
    }
    
}
